package com.quad.innovators.salesease.converter;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.model.bo.SysUserBO;
import com.quad.innovators.salesease.model.common.Option;
import com.quad.innovators.salesease.model.dto.SysUserImportDTO;
import com.quad.innovators.salesease.model.entity.SysRole;
import com.quad.innovators.salesease.model.entity.SysUser;
import com.quad.innovators.salesease.model.form.SysUserForm;
import com.quad.innovators.salesease.model.vo.SysUserInfoVO;
import com.quad.innovators.salesease.model.vo.SysUserPageVO;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;

/**
 * 用户对象转换器
 *
 * @author haoxr
 * @since 2022/6/8
 */
@Mapper(componentModel = "spring")
public interface SysUserConverter {

    SysUserPageVO toPageVo(SysUserBO bo);

    @Mappings({
            @Mapping(target = "items", source = "records"),
            @Mapping(target = "pageInfo.currPage", source = "current"),
            @Mapping(target = "pageInfo.totalCount", source = "total"),
            @Mapping(target = "pageInfo.pageSize", source = "size")
    })
    PageResultBean<SysUserPageVO> toPageVo(Page<SysUserBO> bo);

    @Mappings({
            @Mapping(target = "status", expression = "java(entity.getStatus().getValue())")
    })
    SysUserForm toForm(SysUser entity);

    @InheritInverseConfiguration(name = "toForm")
    @Mappings({
            @Mapping(target = "status", expression = "java(com.quad.innovators.salesease.enums.IBaseEnum.getEnumByValue(entity.getStatus(), com.quad.innovators.salesease.enums.StatusEnum.class))")
    })
    SysUser toEntity(SysUserForm entity);

    @Mappings({
            @Mapping(target = "userId", source = "id")
    })
    SysUserInfoVO toUserInfoVo(SysUser entity);

    SysUser toEntity(SysUserImportDTO vo);

    @Mappings({
            @Mapping(target = "value", source = "userCode"),
            @Mapping(target = "label", source = "username")
    })
    Option<String> entity2Option(SysUser sysUser);

    @Mappings({
            @Mapping(target = "value", source = "userCode"),
            @Mapping(target = "label", source = "username")
    })
    List<Option<String>> entities2Options(List<SysUser> sysUsers);
}
